package site.zhangzhuo.v3;


import java.io.Serial;
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.HashMap;

/*
字
通过读句子来建立字之间的关联
 */
public class Zi implements Serializable {
    @Serial
    private static final long serialVersionUID= 32L;
    private final Character ming;//字名
    //前后字及其和本字之间的连接值
    private final HashMap<Character,HashMap.SimpleEntry<Zi,Integer>> qian=new HashMap<>();//字前
    private final HashMap<Character,HashMap.SimpleEntry<Zi,Integer>> hou=new HashMap<>();//字后
    //以此字打头起的词
    private final HashMap<String,Ci> qici=new HashMap<>();

    public Zi(Character danzi){
        ming=danzi;
    }
    public Character getMing(){
        return ming;
    }
    //添加前字
    public void addQian(Zi zi){
        if(qian.containsKey(zi.getMing())){
            qian.get(zi.getMing()).setValue(qian.get(zi.getMing()).getValue()+1);
        }
        else qian.putIfAbsent(zi.getMing(),new AbstractMap.SimpleEntry<Zi,Integer>(zi,1));
    }
    //添加后字
    public void addHou(Zi zi){
        if(hou.containsKey(zi.getMing())){
            hou.get(zi.getMing()).setValue(hou.get(zi.getMing()).getValue()+1);
        }
        else hou.putIfAbsent(zi.getMing(),new AbstractMap.SimpleEntry<Zi,Integer>(zi,1));
    }
    //前字的数量
    public int sizeOfQian(){
        return qian.size();
    }
    //后字的数量
    public int sizeOfHou()
    {
        return hou.size();
    }
    @Override
    public String toString(){
        StringBuilder str=new StringBuilder();
        str.append(showBlank(ming)+"\t----前：");
        for(Character c:qian.keySet()){
            str.append(showBlank(c));
            str.append(qian.get(c).getValue());
        }
        str.append("----后：");
        for(Character c:hou.keySet()){
            str.append(showBlank(c));
            str.append(hou.get(c).getValue());
        }
        str.append("----");
        return str.toString();
    }
    //输出的时候把空白符转换成可显示字符，像换行空格
    private String showBlank(Character c){
        switch (c){
            case '\n':
                return "换行符";
            case ' ':
                return "半角空格符";
            case '\u3000':
                return "全角空格符";
            case '\t':
                return "制表符";
        }
        return c.toString();
    }
}
